;(function(win){
	var Calendar = {};

	var weeken = ["日","一","二","三","四","五","六"];

	var calendar = document.getElementById("calendar");

	Calendar = {
		CalerderBoxWidth:function(){
			sw = document.body.offsetWidth;
			return sw/7
		},
		checkIn:function(now,Year,Month,Day,ThisMonthFirstWeek){
			var TheBoxs = document.getElementById("TheDays").getElementsByClassName("boxs");
			var today = now.getDate()+ThisMonthFirstWeek-1;
			if(now.getFullYear() == Year && now.getMonth() == Month && now.getDate() == Day){
				TheBoxs[today].getElementsByClassName("boxsInnerNum")[0].setAttribute("class","boxsInnerNum today");
			}
		},
		CalendarInit:function(now,Year,Month,Week,Day){		

			//配制顶部
			var top = document.createElement("div");
			top.setAttribute("class","top");
			calendar.appendChild(top);
			//左按钮
			var prev = document.createElement("button");
			prev.setAttribute("class","prev");
			prev.setAttribute("id","prev");
			prev.innerHTML = "<";
			top.appendChild(prev);
			// 年月
			var YearMonth = document.createElement("span");
			YearMonth.setAttribute("class","YearMonth");
			YearMonth.setAttribute("id","YearMonth");
			YearMonth.innerHTML = Year+"年"+eval(eval(Month)+1)+"月";
			top.appendChild(YearMonth);
			//右按钮
			var next = document.createElement("button");
			next.setAttribute("class","next");
			next.setAttribute("id","next");
			next.innerHTML = ">";
			top.appendChild(next);
			//一周日期
			var week = document.createElement("div");
			week.setAttribute("class","week");
			var clear = document.createElement("div");
			clear.setAttribute("style","clear:both;")

			calendar.appendChild(week);	
			for(var j = 0; j < weeken.length; j++){				
				var boxs = document.createElement("div");	
				boxs.setAttribute("class","boxs");			
				boxs.innerHTML = weeken[j];
				boxs.style.width = this.CalerderBoxWidth() + "px";	
				week.appendChild(boxs);	
			}
			week.appendChild(clear);	
		},
		MonthChange:function(now,Year,Month,Week,Day){
			//当月的第一天
			ThisMonthFirstDay = new Date(Year,Month,1);
			//下一个月份的第一天
			nextMonthFirstDay = new Date(Year,Month+1,1);
			//获取当月的最后一天 28号 29号 30号 31号
			ThisMonthLastDay = (new Date(nextMonthFirstDay.getTime()-1000*60*60*24)).getDate();
			//当月的第一天是星期几
			ThisMonthFirstWeek = new Date(Year,Month,1).getDay();
			//在一个日历里第一个格子出现的日期
			CalenderBoxFirst = new Date(ThisMonthFirstDay.getTime()-1000*60*60*24*ThisMonthFirstWeek).getDate();
			//在一个日历里最后一个格子出现的日期
			CalenderBoxFirst = new Date(ThisMonthFirstDay.getTime()-1000*60*60*24*ThisMonthFirstWeek).getDate();

			//  判断，行数不等于5时；
			var pageDays = ThisMonthFirstWeek+ThisMonthLastDay;			
			var row = Math.ceil(pageDays/7);
			
			//配置日期时间
			var TheDays = document.createElement("div");
				TheDays.setAttribute("class","TheDays");
				TheDays.setAttribute("id","TheDays");
				calendar.appendChild(TheDays);

			//判断，该月第一天是不是周日，该判断影响当月的日期附加状态
			if( ThisMonthFirstWeek == 0){
				prevMonth = 0;
			}else{
				prevMonth = 1;
			}

			for(var i = 0; i < 7*row; i++){
				var boxs = document.createElement("div");
				boxs.setAttribute("class","boxs noActiveStyle");
				var boxsInnerNum = document.createElement("span");
				boxsInnerNum.setAttribute("class","boxsInnerNum");
				boxs.style.width = this.CalerderBoxWidth() + "px";
				
				days = new Date(Year,Month-prevMonth,CalenderBoxFirst+i).getDate();
				months = new Date(Year,Month-prevMonth,CalenderBoxFirst+i).getMonth();

				if(months === Month){
					boxs.setAttribute("class","boxs activeStyle");
				}

				boxsInnerNum.innerHTML = days;
				boxs.appendChild(boxsInnerNum);
				TheDays.appendChild(boxs);
			}

			var clear = document.createElement("div");
			clear.setAttribute("style","clear:both;")
			TheDays.appendChild(clear);


			// 今天签到 按钮 渲染
			this.checkIn(now,Year,Month,Day,ThisMonthFirstWeek);
		},
		timer:function(){
			var self = this;
			//默认  当年  当月  当日
			var now =  new Date()
			var ThisYear = now.getFullYear();
			var ThisMonth = now.getMonth();
			var Week = now.getDay();
			var ThisDate = now.getDate();

			// 日历初始化时，顶部显示年月和周几
			this.CalendarInit(now,ThisYear,ThisMonth,Week,ThisDate);
			// 日历 当月 天数  初始化
			this.MonthChange(now,ThisYear,ThisMonth,Week,ThisDate);


			//  时间 上一个 或 下一个 月份触发
			//prev
			document.getElementById("prev").onclick = function(){
				// document.getElementById("TheDays").removeNode(true);
				document.getElementById("TheDays").parentNode.removeChild(document.getElementById("TheDays"));
				ThisMonth--;
				prevMon = new Date(ThisYear,ThisMonth).getMonth();
				prevFullYear = new Date(ThisYear,ThisMonth).getFullYear();
				document.getElementById("YearMonth").innerHTML = prevFullYear+"年"+eval(eval(prevMon)+1)+"月";
				self.MonthChange(now,prevFullYear,prevMon,Week,ThisDate);
			}
			// next
			document.getElementById("next").onclick = function(){
				// document.getElementById("TheDays").removeNode(true);
				document.getElementById("TheDays").parentNode.removeChild(document.getElementById("TheDays"));
				ThisMonth++;
				prevMon = new Date(ThisYear,ThisMonth).getMonth();
				prevFullYear = new Date(ThisYear,ThisMonth).getFullYear();
				document.getElementById("YearMonth").innerHTML = prevFullYear+"年"+eval(eval(prevMon)+1)+"月";
				self.MonthChange(now,prevFullYear,prevMon,Week,ThisDate);
			}
		}
	}

	win["Calendar"] = Calendar;

})(window);